#include "main.h"
#include "pwm.h"

void pwm_output_set(u32 num);
void pwm_proc(void);

void pwm_init(void)
{
  TM_TimeBaseInitTypeDef MCTM_TimeBaseInitStructure;
  TM_OutputInitTypeDef MCTM_OutputInitStructure;
//   MCTM_CHBRKCTRInitTypeDef MCTM_CHBRKCTRInitStructure;

  {
    CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
    CKCUClock.Bit.AFIO                                = 1;
    CKCUClock.Bit.PB                                  = 1;
    CKCUClock.Bit.MCTM0                               = 1;
    CKCU_PeripClockConfig(CKCUClock, ENABLE);        
  }

  /* Configure MCTM Channel 1 output pin                                                                    */
  AFIO_GPxConfig(PWM_AF_PORT, PWM_AF_PIN, AFIO_FUN_MCTM_GPTM);
//   AFIO_GPxConfig(PWM_AF_PORT, PWM_AF_PIN_N, AFIO_FUN_MCTM_GPTM);

  /* MCTM Time Base configuration                                                                           */
  MCTM_TimeBaseInitStructure.CounterReload = PWM_TM_RELOAD - 1;
  MCTM_TimeBaseInitStructure.Prescaler = 0;
  MCTM_TimeBaseInitStructure.RepetitionCounter = 0;
  MCTM_TimeBaseInitStructure.CounterMode = TM_CNT_MODE_UP;
  MCTM_TimeBaseInitStructure.PSCReloadTime = TM_PSC_RLD_IMMEDIATE;
  TM_TimeBaseInit(HT_MCTM0, &MCTM_TimeBaseInitStructure);

  /* MCTM Channel 1 output configuration                                                              */
  MCTM_OutputInitStructure.Channel = PWM_CH;
  MCTM_OutputInitStructure.OutputMode = TM_OM_PWM1;
  MCTM_OutputInitStructure.Control = TM_CHCTL_ENABLE;
//   MCTM_OutputInitStructure.ControlN = TM_CHCTL_DISABLE;
  MCTM_OutputInitStructure.Polarity = TM_CHP_NONINVERTED;
//   MCTM_OutputInitStructure.PolarityN = TM_CHP_NONINVERTED;
  MCTM_OutputInitStructure.IdleState = MCTM_OIS_HIGH;
//   MCTM_OutputInitStructure.IdleStateN = MCTM_OIS_HIGH;
  MCTM_OutputInitStructure.Compare = PWM_TM_RELOAD;
  MCTM_OutputInitStructure.AsymmetricCompare = 0;
  TM_OutputInit(HT_MCTM0, &MCTM_OutputInitStructure);

  /* MCTM Off State, lock, Break, Automatic Output enable, dead time configuration                          */
//   MCTM_CHBRKCTRInitStructure.OSSRState = MCTM_OSSR_STATE_ENABLE;
//   MCTM_CHBRKCTRInitStructure.OSSIState = MCTM_OSSI_STATE_ENABLE;
//   MCTM_CHBRKCTRInitStructure.LockLevel = MCTM_LOCK_LEVEL_2;
//   MCTM_CHBRKCTRInitStructure.Break0 = MCTM_BREAK_ENABLE;
//   MCTM_CHBRKCTRInitStructure.Break0Polarity = MCTM_BREAK_POLARITY_LOW;
//   MCTM_CHBRKCTRInitStructure.AutomaticOutput = MCTM_CHAOE_ENABLE;
//   MCTM_CHBRKCTRInitStructure.DeadTime = PWM_TM_DEAD_TIME;
//   MCTM_CHBRKCTRInitStructure.BreakFilter = 0;
//   MCTM_CHBRKCTRConfig(HT_MCTM0, &MCTM_CHBRKCTRInitStructure);

  /* MCTM counter enable                                                                                    */
  TM_Cmd(HT_MCTM0, ENABLE);

  /* MCTM Channel Main Output enable                                                                        */
  MCTM_CHMOECmd(HT_MCTM0, ENABLE);
}

//PWM比例输出，0 ~ 1000
void pwm_output_set(u32 num)
{
    if(num > PWM_MAX)num = PWM_MAX;
    if(num <= 0)num = 0;

    num = num * PWM_CONST;

    TM_SetCaptureCompare(HT_MCTM0,PWM_CH,num);
}

void pwm_proc(void)
{
    static u32 pwm_num = 0;
    static u8 flag = true;

    if(flag)
    {
        pwm_num += 5;
        pwm_output_set(pwm_num);
        if(pwm_num >= PWM_MAX)flag = !flag;
    }
    else
    {
        pwm_num -= 5;
        pwm_output_set(pwm_num);
        if(pwm_num <= 100)flag = !flag;
    }
}
